/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO : think about improvements, of a wrapper for a full form validation (with checkboxes etc)
package martian.m4gic.form 
{
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	public class Form
	{
		static public const ALPHABETIC:String = "alphabetic";
		static public const NUMERIC:String = "numeric";
		static public const ALPHANUMERIC:String = "alphanumeric";
		static public const FRENCH:String = "french";
		static public const EMAIL:String = "email";
		static public const LOGIN:String = "login";
		static public const PASSWORD:String = "password";
		static public const PHONE:String = "phone";
		static public const MOBILE:String = "mobile";
		static public const ZIPCODE:String = "zipcode";
		static public var CUSTOM:Dictionary = new Dictionary();
		
		static private var regexs:Dictionary;
		static private var restricts:Dictionary;
		
		static private function feed():void
		{
			regexs = new Dictionary();
				regexs[ALPHABETIC]			= new RegExp("^[a-zA-Z\s]+$", "g");
				regexs[NUMERIC]				= new RegExp("^[0-9]+$", "g");
				regexs[ALPHANUMERIC]		= new RegExp("^[a-zA-Z\s0-9]+$", "g");
				regexs[FRENCH]				= new RegExp("[a-zA-Z0-9àéèêëïôùç\\-',\s]+", "g");
				regexs[EMAIL] 				= new RegExp("^(([a-zA-Z0-9\._-]+)@([a-zA-Z0-9-]+)\.([a-zA-Z]{2,4}))$", "g");
				regexs[LOGIN]				= new RegExp("^[a-z0-9\\-._]+$", "g");
				regexs[PASSWORD]			= regexs[ALPHANUMERIC];
				regexs[PHONE]				= new RegExp("^(0[0-9]{1})([0-9]{2}){4}$", "g");
				regexs[MOBILE]			= new RegExp("^(06)([0-9]{2}){4}$", "g");
				regexs[ZIPCODE]				= new RegExp("^[0-9]{5}$", "g");
				
			restricts = new Dictionary();
				restricts[ALPHABETIC]		= "a-z A-Z";
				restricts[NUMERIC]			= "0-9";
				restricts[ALPHANUMERIC]		= restricts[ALPHABETIC] + restricts[NUMERIC];
				restricts[FRENCH]			= restricts[ALPHANUMERIC] + " \\-', àéèêëïôùç ";
				restricts[EMAIL]			= "a-z0-9\\-_@.";
				restricts[LOGIN]			= "a-z0-9\\-._";
				restricts[PASSWORD]			= restricts[ALPHANUMERIC];
				restricts[PHONE]			= restricts[NUMERIC];
				restricts[MOBILE]		= restricts[NUMERIC];
				restricts[ZIPCODE]			= restricts[NUMERIC];
		}
		
		static public function add(policy:String, regex:RegExp, restrict:String):Boolean
		{
			if (regexs == null || restricts == null) { feed(); }
			
			if (CUSTOM[policy] != null) { return false; }
			
			CUSTOM[policy] = "custom_" + policy;
			regexs[CUSTOM[policy]] = regex;
			restricts[CUSTOM[policy]] = restrict;
			
			return true;
		}
		
		static public function update(policy:String, regexs:RegExp, restrict:String):Boolean
		{
			if (regexs == null || restricts == null) { feed(); }
			
			if (CUSTOM[policy] == null) { return false; }
			
			regexs[CUSTOM[policy]] = regexs;
			restricts[CUSTOM[policy]] = restrict;
			
			return true;
		}
		
		static public function remove(policy:String):Boolean
		{
			if (regexs == null || restricts == null) { feed(); }
			
			if (CUSTOM[policy] == null) { return false; }
			
			delete regexs[CUSTOM[policy]];
			delete restricts[CUSTOM[policy]];
			delete CUSTOM[policy];
			
			return true;
		}
		
		static public function check(str:String, policy:String):Boolean { return (str.search(regexs[policy]) != -1); }
		
		private var tfs:Array;
			public function get items():Array { return tfs; }
		
		private var elements:Dictionary;
		private var length:int;
		private var offset:int;
			public function get nextTabIndex():int { return offset + tfs.length; }
			
		private var _errors:Array;
			public function get errors():Array { return _errors; }
		
		public function Form(tabIndex:int = 0)
		{
			if (regexs == null || restricts == null) { feed(); }
			
			tfs = new Array();
			elements = new Dictionary(true);
			length = 0;
			offset = tabIndex;
		}
		
		public function add(tf:TextField, policy:String, optional:Boolean = false, minChars:int = 0, maxChars:int = -1):Boolean
		{
			if (elements[tf] == null)
			{
				tf.text = "";
				tf.restrict = restricts[policy];
				tf.displayAsPassword = (policy == PASSWORD);
				tf.tabIndex = offset + length++;
				
				if (maxChars >= minChars) { tf.maxChars = maxChars; }
				
				tfs.push(tf);
				
				elements[tf] = new Object;
					elements[tf].policy = policy; 
					elements[tf].optional = optional;
					elements[tf].minChars = minChars;
					
				return true;
			}
			
			return false;
		}
		
		public function update(tf:TextField, policy:String, optional:Boolean = false, minChars:int = 0, maxChars:int = -1):Boolean
		{
			for (var element:* in elements)
			{
				if (element == tf)
				{
					tf.restrict = restricts[policy];
					tf.displayAsPassword = (policy == PASSWORD);
					if (maxChars > minChars) { tf.maxChars = maxChars; }
					
					elements[tf] = new Object();
						elements[tf].policy = policy; 
						elements[tf].optional = optional;
						elements[tf].minChars = minChars;
						
					return true;
				}
			}
			
			return false;
		}
		
		public function remove(tf:TextField):Boolean
		{
			for (var element:* in elements)
			{
				if (element == tf)
				{
					tf.restrict = "";
					tf.displayAsPassword = false;
					tf.maxChars = 0;
					
					delete elements[tf];
					return true;
				}
			}
			
			return false;
		}
		
		public function validate():Boolean
		{
			_errors = new Array();
			
			for (var element:* in elements)
			{
				if ((element.text != "") || (!elements[element].optional))
				{
					if (!check(element.text, elements[element].policy) || (element.text.length < elements[element].minChars)) { _errors.push(element); }
				}
			}
			
			_errors = _errors.sortOn("tabIndex", Array.NUMERIC);
			return (_errors.length == 0);
		}
	}
}